package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataAddGetDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.draft.pojo.dto.DraftDeleteDTO;
import com.xbongbong.pro.draft.pojo.dto.DraftGetDTO;
import com.xbongbong.pro.draft.pojo.dto.DraftSaveDTO;
import com.xbongbong.pro.draft.pojo.vo.DraftDeleteVO;
import com.xbongbong.pro.draft.pojo.vo.DraftExistVO;
import com.xbongbong.pro.draft.pojo.vo.DraftGetVO;
import com.xbongbong.pro.draft.pojo.vo.DraftSaveVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.FormDataDraftEntity;
import com.xbongbong.saas.model.FormDataDraftModel;
import com.xbongbong.saas.service.FormDataDraftService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 表单草稿Service层实现
 *
 * @author GuoJun.HU
 * @date 2019/2/20 9:50
 * @since V1.0
 */
@Service("formDataDraftService")
public class FormDataDraftServiceImpl implements FormDataDraftService {

    @Resource
    private FormDataDraftModel formDataDraftModel;

    /**
     * 保存草稿
     *
     * @param draftSaveDTO 保存草稿的数据
     * @return DraftSaveVO
     * @author GuoJun.HU
     * @date 2019/2/20 10:02
     * @since V1.0
     */
    @Override
    public DraftSaveVO save(DraftSaveDTO draftSaveDTO) throws XbbException {
        String userId = draftSaveDTO.getUserId();
        Map<String, Object> param = BeanUtil.convertBean2Map(draftSaveDTO, true);
        param.put("userId", userId);
        @SuppressWarnings("unchecked")
        List<FormDataDraftEntity> dataDraftList = formDataDraftModel.findEntitys(param);

        FormDataDraftEntity formDataDraft;
        if (dataDraftList.isEmpty()) {
            // 没有草稿则直接新建保存
            formDataDraft = new FormDataDraftEntity();
            BeanUtil.copyProperties(draftSaveDTO, formDataDraft);
            formDataDraft.setData(JSON.parseObject(draftSaveDTO.getData()));
            formDataDraftModel.insert(formDataDraft);
        } else if (Objects.equals(dataDraftList.size(), 1)) {
            // 已经有草稿了则编辑保存
            formDataDraft = dataDraftList.get(0);
            formDataDraft.setData(JSON.parseObject(draftSaveDTO.getData()));
            formDataDraftModel.update(formDataDraft);
        } else {
            // 已经有多条草稿，应该是异常情况，删除原来的草稿后新建保存
            List<Long> idList = new ArrayList<>();
            dataDraftList.forEach(item -> idList.add(item.getId()));
            formDataDraftModel.deleteBatch(idList);

            formDataDraft = new FormDataDraftEntity();
            BeanUtil.copyProperties(draftSaveDTO, formDataDraft);
            formDataDraft.setData(JSON.parseObject(draftSaveDTO.getData()));
            formDataDraftModel.insert(formDataDraft);
        }

        DraftSaveVO draftSaveVO = new DraftSaveVO();
        draftSaveVO.setId(formDataDraft.getId());
        return draftSaveVO;
    }

    /**
     * 获取草稿
     *
     * @param draftGetDTO 获取草稿的参数
     * @return DraftGetVO
     * @author GuoJun.HU
     * @date 2019/2/20 10:04
     * @since V1.0
     */
    @Override
    public DraftGetVO get(DraftGetDTO draftGetDTO) throws XbbException {
        String userId = draftGetDTO.getUserId();
        Map<String, Object> param = BeanUtil.convertBean2Map(draftGetDTO, true);
        param.put("userId", userId);
        @SuppressWarnings("unchecked")
        List<FormDataDraftEntity> dataDraftList = formDataDraftModel.findEntitys(param);

        DraftGetVO draftGetVO = new DraftGetVO();
        if (Objects.equals(dataDraftList.size(), 1)) {
            // 已经有草稿了则编辑保存
            FormDataDraftEntity formDataDraft = dataDraftList.get(0);
            BeanUtil.copyProperties(formDataDraft, draftGetVO);
            draftGetVO.setData(JSON.toJSONString(formDataDraft.getData()));
        }

        return draftGetVO;
    }

    /**
     * 删除草稿
     *
     * @param draftDeleteDTO 删除草稿的参数
     * @return DraftDeleteVO
     * @author GuoJun.HU
     * @date 2019/2/20 10:05
     * @since V1.0
     */
    @Override
    public DraftDeleteVO delete(DraftDeleteDTO draftDeleteDTO) throws XbbException {
        DraftGetDTO draftGetDTO = new DraftGetDTO();
        BeanUtil.copyProperties(draftDeleteDTO, draftGetDTO);
        DraftGetVO draftGetVO = get(draftGetDTO);
        if (draftGetVO != null && draftGetVO.getId() != null && draftGetVO.getId() > 0) {
            String corpid = draftDeleteDTO.getCorpid();
            formDataDraftModel.deleteByKey(draftGetVO.getId(), corpid);
            DraftDeleteVO draftDeleteVO = new DraftDeleteVO();
            draftDeleteVO.setId(draftGetVO.getId());
            return draftDeleteVO;
        } else {
            // 删除的数据不存在
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    /**
     * 新建表单时判断是否有草稿
     *
     * @param formDataAddGetDTO 新建表单数据
     * @return DraftExistVO
     * @author GuoJun.HU
     * @date 2019/2/20 15:43
     * @since V1.0
     */
    @Override
    public DraftExistVO exist(FormDataAddGetDTO formDataAddGetDTO) throws XbbException {
        DraftExistVO draftExistVO = new DraftExistVO();
        DraftGetDTO draftGetDTO = new DraftGetDTO();
        BeanUtil.copyProperties(formDataAddGetDTO, draftGetDTO);
        DraftGetVO draftGetVO = get(draftGetDTO);
        if (draftGetVO != null && draftGetVO.getId() != null && draftGetVO.getId() > 0) {
            draftExistVO.setHasDraft(true);
            draftExistVO.setDraftId(draftGetVO.getId());
        }
        return draftExistVO;
    }

    /**
     * 新建表单保存成功后删除草稿
     *
     * @param formDataAddDTO 新建传参
     * @author GuoJun.HU
     * @date 2019/2/21 10:18
     * @since V1.0
     */
    @Override
    public void deleteAfterSave(FormDataAddDTO formDataAddDTO) throws XbbException {
        FormDataAddGetDTO formDataAddGetDTO = new FormDataAddGetDTO();
        BeanUtil.copyProperties(formDataAddDTO, formDataAddGetDTO);
        DraftExistVO draftExistVO = exist(formDataAddGetDTO);
        if (draftExistVO != null && draftExistVO.getHasDraft()) {
            Long draftId = draftExistVO.getDraftId();
            String corpid = formDataAddDTO.getCorpid();
            formDataDraftModel.deleteByKey(draftId, corpid);
        }
    }
}
